
import backend.app.app.models.db_sys_models as md
from backend.app.app.db.session_sys import session

# 根据LegalPerson_Code删除tb_license表中
# tb_license表
def license_del(legalperson_code):
    try:
        session.query(md.TbLicense).filter(md.TbLicense.LegalPeson_Code == legalperson_code).delete()
        session.commit()
        print('删除授权人成功')
        return True
    except:
        print('删除授权人失败')
        session.rollback()
        return False



# 根据LegalPerson_Code更新tb_license表
def license_update(update_data:list):
    try:
        session.query(md.TbLicense).filter(md.TbLicense.LegalPeson_Code == update_data[1]).update(
            {md.TbLicense.LegalPeson_Name:update_data[0],
             md.TbLicense.LegalPeson_Code:update_data[1],
             md.TbLicense.LegalPeson_Representative:update_data[2],
             md.TbLicense.Auth_serial_number:update_data[3],
             md.TbLicense.IsQualified:update_data[4],
             md.TbLicense.Authed_StartDate:update_data[5],
             md.TbLicense.Authed_EndDate:update_data[6],
             md.TbLicense.Authed_Online_Number:update_data[7],
             md.TbLicense.User_DB_constr:update_data[8],
             md.TbLicense.License_Remarks:update_data[9]
             })
        session.commit()
        print('更新授权人成功')
        return True
    except:
        print('更新授权人失败')
        session.rollback()
        return False

#查询id，实现自增
def all_id():
    try:
        all_id = session.query(md.TbLicense.LegalPerson_ID).all()
        return all_id
    except:
        print("查询license id失败")
        session.rollback()
        return False

# 返回tb_license这个表所有内容
def all_tblicense():
    try:
        tblicense_data = session.query(md.TbLicense.LegalPerson_ID,
                                       md.TbLicense.LegalPeson_Name,
                                       md.TbLicense.LegalPeson_Code,
                                       md.TbLicense.LegalPeson_Representative,
                                       md.TbLicense.Auth_serial_number,
                                       md.TbLicense.IsQualified,
                                       md.TbLicense.Authed_StartDate,
                                       md.TbLicense.Authed_EndDate,
                                       md.TbLicense.Authed_Online_Number,
                                       md.TbLicense.User_DB_constr,
                                       md.TbLicense.Auth_devname,
                                       md.TbLicense.License_Remarks,
                                       ).all()
        return tblicense_data
    except:
        print('查找tb_license失败')
        session.rollback()
        return False

# 添加新授权人信息
# tb_license表
def add_newlegalperson(legalperson_id,
                       legalperson_name,
                       legalperson_code,
                       legalperson_representative,
                       auth_serial_number,
                       authed_stardate,
                       authed_enddate,
                       user_db_constr,
                       authed_online_number,
                       isqualified,
                        license_remarks,
                       auth_devname,
                       ):
    new_legalperson = md.TbLicense(LegalPerson_ID=legalperson_id,
                                   LegalPeson_Name=legalperson_name,
                                   LegalPeson_Code=legalperson_code,
                                   LegalPeson_Representative=legalperson_representative,
                                   Auth_serial_number=auth_serial_number,
                                   IsQualified=isqualified,
                                   Authed_StartDate=authed_stardate,
                                   Authed_EndDate=authed_enddate,
                                   Authed_Online_Number=authed_online_number,
                                   User_DB_constr=user_db_constr,
                                   Auth_devname=auth_devname,
                                   License_Remarks=license_remarks
                                   )
    try:
        session.add(new_legalperson)
        session.commit()
        print('新建授权人成功')
        session.close()
        return True
    except:
        print('新建授权人失败')
        session.rollback()
        return False
